﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ECommerce.Library.Web;
using System.Collections.Generic;
using ECommerce.Library.DataStructure;
using AutoGear.BLL;
using ECommerce.Library;
using eShop.Model;
using eShop.Business;

public partial class controls_wucBrandProducts : BaseControl
{
    private int _repeatedColumns = 0;
    private int _maxItem = 0;
    private int _productBrandId;
    private int _totalPage;
    private int _pageSize;
    private int _currentPageIndex;
    private int _totalProduct;
    private int _startRow;

    public int RepeatedColumns
    {
        get { return _repeatedColumns; }
        set { _repeatedColumns = value; }
    }
    public int MaxItem
    {
        get { return _maxItem; }
        set { _maxItem = value; }
    }
    public int PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }
    public int ProductBrandId
    {
        get { return this._productBrandId; }
        set { this._productBrandId = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                SetPagingParameter(1);
                LoadRepeaterItems();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    protected void SetPagingParameterToViewState()
    {
        ViewState["repeatedColumns"] = _repeatedColumns.ToString();
        ViewState["maxItem"] = _maxItem.ToString();
        ViewState["totalPage"] = _totalPage.ToString();
        ViewState["pageSize"] = _pageSize.ToString();
        ViewState["currentPageIndex"] = _currentPageIndex.ToString();
        ViewState["totalProduct"] = _totalProduct.ToString();
        ViewState["productBrandId"] = _productBrandId.ToString();
    }
    protected void GetPagingParameterFromViewState()
    {
        _repeatedColumns = Convert.ToInt32(ViewState["repeatedColumns"].ToString());
        _maxItem = Convert.ToInt32(ViewState["maxItem"].ToString());
        _totalPage = Convert.ToInt32(ViewState["totalPage"].ToString());
        _pageSize = Convert.ToInt32(ViewState["pageSize"].ToString());
        _currentPageIndex = Convert.ToInt32(ViewState["currentPageIndex"].ToString());
        _totalProduct = Convert.ToInt32(ViewState["totalProduct"].ToString());
        _productBrandId = Convert.ToInt32(ViewState["productBrandId"].ToString());
    }
    protected void SetPagingParameter(int currentPageIndex)
    {
        if (_productBrandId == 0)
        {
            throw new ArgumentNullException("_productBrandId in controls_wucBrandProducts.SetPagingParameter");
        }

        Manufacturer brand = ManufacturerManager.GetById(_productBrandId);
        _totalProduct = ManufacturerManager.GetActiveBrandProductsCount(brand.ProductBrandId);
        _totalPage = _totalProduct % _pageSize != 0 ? (_totalProduct / _pageSize) + 1 : _totalProduct / _pageSize;
        _currentPageIndex = currentPageIndex == 0 ? 1 : currentPageIndex;
        _startRow = (_currentPageIndex - 1) * _pageSize;
    }

    protected void LoadRepeaterItems()
    {
        List<int> pagingList = new List<int>();
        for (int i = 0; i < _totalPage; i++)
        {
            pagingList.Add(i + 1);
        }
        rptPaging.DataSource = pagingList;
        rptPaging.DataBind();

        ReadWriteCustomCollection<Product> products = ManufacturerManager.GetActiveBrandProductsPaged(_productBrandId, "DisplayOrder", _currentPageIndex, _pageSize);
        int startIndex = products.Count == 0 ? 0 : _startRow + 1;
        lblPagingInformation.Text = "Displaying " + startIndex.ToString() + " to " + ((int)(_startRow + products.Count)).ToString() + " (of " + _totalProduct.ToString() + " products)";

        ReadWriteCustomCollection<Product> tempProducts;
        List<ReadWriteCustomCollection<Product>> list = new List<ReadWriteCustomCollection<Product>>();
        tempProducts = new ReadWriteCustomCollection<Product>();
        int count = 0;
        foreach (Product product in products)
        {
            tempProducts.Add(product);
            count++;
            if (count % RepeatedColumns == 0)
            {
                list.Add(tempProducts);
                tempProducts = new ReadWriteCustomCollection<Product>();
            }
        }
        if (tempProducts.Count > 0)
        {
            list.Add(tempProducts);
        }

        rptVertical.DataSource = list;
        rptVertical.DataBind();

        SetPagingParameterToViewState();

        if (_currentPageIndex == 1)
        {
            DisableLinkButtonStyle(lkbPrev);
            EnableLinkButtonStyle(lkbNext);
        }
        else if (_currentPageIndex == _totalPage)
        {
            DisableLinkButtonStyle(lkbNext);
            EnableLinkButtonStyle(lkbPrev);
        }
        else
        {
            EnableLinkButtonStyle(lkbPrev);
            EnableLinkButtonStyle(lkbNext);
        }
        if (_totalProduct <= _pageSize)
        {
            DisableLinkButtonStyle(lkbNext);
        }
    }

    protected void EnableLinkButtonStyle(LinkButton lkb)
    {
        lkb.Style["color"] = "#535353";
        lkb.Style["cursor"] = "pointer";
        lkb.OnClientClick = "javascript:return true;";
    }

    protected void DisableLinkButtonStyle(LinkButton lkb)
    {
        lkb.Style["color"] = "#B0B0B0";
        lkb.Style["cursor"] = "default";
        lkb.OnClientClick = "javascript:return false;";
    }

    protected void Vertical_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptHorizontal = (Repeater)e.Item.FindControl("rptHorizontal");
                rptHorizontal.DataSource = (ReadWriteCustomCollection<Product>)e.Item.DataItem;
                rptHorizontal.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void Horizontal_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Product product = (Product)e.Item.DataItem;
                Label lblProductName = (Label)e.Item.FindControl("lblProductName");
                lblProductName.Text = product.ProductName;
                Image imgProductImage = (Image)e.Item.FindControl("imgProductImage");
                imgProductImage.ImageUrl = ImageResizer.CheckImageMissing(product.ProductImageLargeThumbnail, SiteSettings.NoImageLargeThumbnailUrl);                
                Label lblPrice = (Label)e.Item.FindControl("lblPrice");
                lblPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.ProductPrice);
                HyperLink hlkProductDetail = (HyperLink)e.Item.FindControl("hlkProductDetail");
                hlkProductDetail.NavigateUrl = product.ProductUrl;
                hlkProductDetail.Text = string.Empty;
                Image imgSpecial = (Image)e.Item.FindControl("imgSpecial");
                imgSpecial.Visible = product.IsSpecial;

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                int pageIndex = (int)e.Item.DataItem;
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                if (_currentPageIndex == pageIndex)
                {
                    DisableLinkButtonStyle(lkbPageIndex);
                }
                else
                {
                    EnableLinkButtonStyle(lkbPageIndex);
                }
                lkbPageIndex.Text = pageIndex.ToString();
                lkbPageIndex.CommandArgument = pageIndex.ToString();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            LinkButton lkbPageIndex = (LinkButton)e.CommandSource;
            GetPagingParameterFromViewState();
            SetPagingParameter(Convert.ToInt32(lkbPageIndex.CommandArgument.ToString()));
            LoadRepeaterItems();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void lkbPrev_Click(object sender, EventArgs e)
    {
        try
        {
            GetPagingParameterFromViewState();
            _currentPageIndex = _currentPageIndex > 1 ? _currentPageIndex - 1 : 1;
            SetPagingParameter(_currentPageIndex);
            LoadRepeaterItems();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void lkbNext_Click(object sender, EventArgs e)
    {
        try
        {
            GetPagingParameterFromViewState();
            _currentPageIndex = _currentPageIndex < _totalPage ? _currentPageIndex + 1 : _totalPage;
            SetPagingParameter(_currentPageIndex);
            LoadRepeaterItems();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
